/* $Id: ServletAlbum.java,v 1.2 1999/03/17 02:21:36 duncan Exp $
 *
 */

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * A Servlet to help view jpg images
 *
 * @author James Chy
 */

public class ServletAlbum extends HttpServlet {

    String title, currpath;
    ResourceBundle rb;
    boolean showQuery;
    PrintWriter out;
    String connection;

    static final long serialVersionUID = 0;
    
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
        rb = ResourceBundle.getBundle("LocalStrings");
        title = rb.getString("servletAlbum.title");
        showQuery = rb.getString("servletAlbum.showQuery").equals("yes");
        currpath = rb.getString("servletAlbum.start");
		connection = rb.getString("servletAlbum.connection");
    }
        

    public ResultSet executeQuery(String query)
    {
        ResultSet rs=null;
        try
        {
            Class.forName("org.gjt.mm.mysql.Driver").newInstance();
            java.sql.Connection conn;
            conn = DriverManager.getConnection(connection);
//                   "jdbc:mysql://localhost/test?user=jimchy&password=lavacat1");
            Statement stmt;

            stmt=conn.createStatement();
            // if(showQuery) out.println("Query: " + query + "<BR>");
                  
            rs = stmt.executeQuery(query);

        }
        catch(ClassNotFoundException cnfe)
        {
            out.println("Class Not Found Exception "+cnfe.toString());
        }
        
        catch(InstantiationException ie)
        {
            out.println("Instantiation Exception "+ie.toString());
        }        
        catch(IllegalAccessException iae)
        {
            out.println("Illegal Access Exception "+iae.toString());
        }
        catch(SQLException sqle)
        {
            out.println("SQL Exception "+sqle.toString());
        }

        return rs;
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        out = response.getWriter();

        int newImageCount = 0;

        out.println("<html>");
        out.println("<body bgcolor=\"white\">");
        out.println("<head>");

		out.println("<title>" + title + "</title>");  
        out.println("</head>");
        out.println("<body>");

		// note that all links are created to be relative. this
		// ensures that we can move the web application that this
		// servlet belongs to to a different place in the url
		// tree and not have any harmful side effects.

        try
        {
		    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
            java.sql.Connection conn;
            conn = DriverManager.getConnection(connection);
//		   "jdbc:mysql://localhost/test?user=jimchy&password=lavacat1");

           
            if(showQuery) out.println("Currpath: " + currpath + "<BR>");

            File main = new File(currpath);

            if(showQuery) out.println("Directory: " + main.isDirectory() +
                                      " Exists: " + main.exists() + "<BR>");
            
            if(main.isDirectory() && main.exists())  
            {
                String[] contents = main.list();
            
                if(showQuery)  out.println("contents.length = " + contents.length + "<BR>");

                content_block:
                for( int i = 0; i < contents.length; i++)
                {
                    if(!contents[i].endsWith(".jpg") && !contents[i].endsWith(".JPG") ) 
                       continue content_block;
            

                    String nextpath = currpath + "\\" + contents[i];
                    File aFile = new File(nextpath);
    
                    if(showQuery)  out.println("contents[" + i + "] = " + contents[i] + "<BR>");

                    if(!aFile.isDirectory())
                    {
                         String query = "SELECT COUNT(*) FROM images, image_date " +
                                        "WHERE images.image_file = \"" + contents[i] + "\" " +
                                        "AND image_date.image_uid = images.uid";
                         if(showQuery) out.println("Query: " + query + "<BR>");
    
                         Statement stmt = conn.createStatement();
                         ResultSet rs = stmt.executeQuery(query);
                         rs.next();
                         int count = rs.getInt("COUNT(*)");
                         if (count == 0)
                         {
                             query = "SELECT COUNT(*) FROM images " +
                                     "WHERE images.image_file = \"" + contents[i] + "\"";
                             if(showQuery) out.println("Query: " + query + "<BR>");
    
                             stmt = conn.createStatement();
                             rs = stmt.executeQuery(query);
                             rs.next();
                             count = rs.getInt("COUNT(*)");
                             
                             if(count==0)
                             {
                                 query = "INSERT images " +
                                         "SET images.image_file = \"" + contents[i] + "\"";
                                 if(showQuery) out.println("Query: " + query + "<BR>");
                                 stmt = conn.createStatement();
                                 rs = stmt.executeQuery(query);
                             }
                             newImageCount++;
                             out.print("<A action=post href=\"OneImage?image=" + contents[i] + "\">");
		             out.print("<img src=\"/thumbs/" + contents[i] + "\">");
                             out.print("</A>");
                             out.println("<BR>");
                         }
                     }
                 }
             }
        }
        catch(ClassNotFoundException cnfe)
        {
            out.println("Class Not Found Exception " + cnfe.getMessage());
        }
        
        catch(InstantiationException ie)
        {
            out.println("Instantiation Exception " + ie.getMessage());
        }	
        catch(IllegalAccessException iae)
        {
            out.println("Illegal Access Exception");
        }
        catch(SQLException sqle)
        {
            out.println("SQL Exception");
        }

        out.println("Records Found: " + newImageCount + "<BR>");


        out.println("</body>");
        out.println("</html>");
    }
}



